layui.use(['table', 'admin', 'ax','element'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var BasicPrivatecar = {
        tableId: "basicPrivatecarTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPrivatecar.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'ID'},
            {align: 'center', toolbar: '#tableBar', title: 'Operation', width:180},
            {field: 'createTime', sort: true, title: 'Dispatch Time', style:'font-weight: 600;',width: 170},
            {field: 'dispatchNumber', title: 'Transport No.', style:'font-weight: 600;',width: 180},
            {field: 'carrierName', sort: true, title: 'Carrier', style:'font-weight: 600;'},
            {field: 'carnumbger', title: 'License Plate', style:'font-weight: 600;color: #19be6b;'},

            {field: 'driver1', title: 'Driver', style:'font-weight: 600;'},
            {field: 'cartel', title: 'Contact Info', style:'font-weight: 600;'},

            {field: 'privatecarType', title: 'Vehicle Type', style:'font-weight: 600;'},
            {field: 'privatecarSurevolume', title: 'Allowed Volume m³', style:'font-weight: 600;'},
            {field: 'privatecarSurevolume', title: 'Load Rate %', style:'font-weight: 600;#ffaa00;', totalRowText: 'Total: ',
            	templet: function(d){
            		return (d.tooDeployVolume/d.privatecarSurevolume*100).toFixed(1)
            	}
		},
            {field: 'tooDeployVolume', sort: true, title: 'Configured Volume', style:'background-color: #dbf1e1;font-weight: 600;', totalRow: true},
            {field: 'tooDeployNum', sort: true, title: 'Configured Quantity', style:'background-color: #dbf1e1;font-weight: 600;', totalRow: true},
            {field: 'lineName', sort: true, title: 'Route', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicPrivatecar.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicPrivatecar.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPrivatecar.tableId,
        url: Feng.ctxPath + '/orderTransportDispatch/findCarListBySpecTooZhuijia',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicPrivatecar.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicPrivatecar.search();
    });

    // 工具条点击事件
    table.on('tool(' + BasicPrivatecar.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'xianze') {
			let gids = []
			let bids = []
			let nums = []
			let orders = []
			let arrorders = []
            for (var i = 0; i < parent.list2.length; i++) {
				if(parent.list2[i].goodId != ''){
					gids.push(parent.list2[i].goodId)
					bids.push(parent.list2[i].batchnumber)
					nums.push(parent.list2[i].num)
					orders.push(parent.list2[i].transportNumber)
				} else {
					arrorders.push(parent.list2[i].transportNumber)
				}
			}
            layer.confirm('Confirm to add to this vehicle?', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/orderTransportDispatch/batchassignDispatchNumber?gids="+gids+"&bids="+bids+"&nums="+nums+"&orders="+orders+"&arrorders="+arrorders,
					data:{dispatchNumber:data.dispatchNumber},
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('Submitting... Please wait patiently', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1透明度的白色背景
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg);
                		if (data.success) {
							parent.submit = 1
                        	Feng.success("Submit successful!");
					    	var Index = parent.layer.getFrameIndex(window.name);
					        parent.layer.close(Index);
						} else {
  		                    Feng.error("Submit failed！"+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg);
                        Feng.error("Submit failed！" + data.responseJSON.message)
                	}
                })
            });
        } else if (layEvent === 'tomingxi'){
            layer.open({
                type: 2,
                title: 'Query Configuration Details',
                area: ['900px', '480px'],
                anim: 5,
                content: Feng.ctxPath + '/orderTransportDispatch/tochecktooy?dispatchNumber=' + data.dispatchNumber,
            });
		}
    });

});
